package com.gitee.ly.api;

import com.gitee.ly.model.JsonResult;
import com.gitee.ly.ServiceManage;
import com.gitee.ly.model.ChangeCanUseBody;
import com.gitee.ly.model.OffLineBody;
import com.gitee.ly.model.RegistryBody;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.*;

//服务注册/下线 API
@Slf4j
@Controller
public class RefistryApi {

    //注册
    @Post
    @Mapping(path = "/registry")
    public JsonResult<String> registry(@Body RegistryBody body) {
        ServiceManage.add(body.convertToServiceInfo());
        return JsonResult.result("", 200, "success");
    }

    //下线
    @Post
    @Mapping(path = "/offline")
    public JsonResult<String> offline(@Body OffLineBody body) {
        ServiceManage.remove(body.getName(), body.getIp(), body.getPort());
        return JsonResult.result("", 200, "success");
    }

    //将状态改为能使用
    @Get
    @Mapping(path = "/canUse")
    public JsonResult<String> canUse(@Body ChangeCanUseBody body) {
        ServiceManage.changeCanUse(body.getName(), body.getIp(), body.getPort(), true);
        return JsonResult.result("", 200, "success");
    }

    //将状态改为不能使用
    @Get
    @Mapping(path = "/canNotUse")
    public JsonResult<String> canNotUse(@Body ChangeCanUseBody body) {
        ServiceManage.changeCanUse(body.getName(), body.getIp(), body.getPort(), false);
        return JsonResult.result("", 200, "success");
    }
}